module.exports = (sequelize, DataTypes) => {
  const Resource = sequelize.define('Resource', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    userId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'users',
        key: 'id'
      }
    },
    type: {
      type: DataTypes.ENUM('design', 'image', 'video', 'exercise', 'paper', 'custom'),
      allowNull: false,
      comment: '资源类型：教学设计/图像/视频/习题/试卷/自定义'
    },
    content: {
      type: DataTypes.JSON,
      allowNull: false,
      comment: '资源内容，JSON格式存储'
    },
    generateTime: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW
    },
    isSaved: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      comment: '是否存入资源库'
    }
  }, {
    tableName: 'resources',
    timestamps: true,
    createdAt: 'generateTime',
    updatedAt: false
  });

  Resource.associate = (models) => {
    // 资源与用户的多对一关系
    Resource.belongsTo(models.User, {
      foreignKey: 'userId',
      as: 'user'
    });

    // 资源与反馈的一对多关系
    Resource.hasMany(models.Feedback, {
      foreignKey: 'resourceId',
      as: 'feedbacks'
    });
  };

  return Resource;
};